package com.shaobufan.museum.controllar;

import com.shaobufan.museum.common.BaseResponse;
import com.shaobufan.museum.common.ErrorCode;
import com.shaobufan.museum.common.ResultUtils;
import com.shaobufan.museum.pojo.request.user.UserLoginRequest;
import com.shaobufan.museum.pojo.request.user.UserRegisterRequest;
import com.shaobufan.museum.pojo.request.usersch.UserScheduleModifyRequest;
import com.shaobufan.museum.pojo.vo.ScheduleVo;
import com.shaobufan.museum.pojo.vo.UserExhibitMomentVo;
import com.shaobufan.museum.pojo.vo.UserScheduleVo;
import com.shaobufan.museum.pojo.vo.UserVo;
import com.shaobufan.museum.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.shaobufan.museum.contant.UserContent.*;
import static com.shaobufan.museum.contant.UserScheduleContent.*;

/**
 * 用户请求
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    UserScheduleService userScheduleService;
    @Resource
    ScheduleService scheduleService;
    @Resource
    UserVerificationService userVerificationService;
    @Resource
    UserExhibitMomentService userExhibitMomentService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public BaseResponse<UserVo> userLogin(@RequestBody UserLoginRequest userLoginRequest) {
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        UserVo userVo = userService.userLogin(userAccount, userPassword);
        if (userVo == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, USER_LOGIN_FAIL, USER_LOGIN_NO_ACCOUNT);
        }
        return ResultUtils.success(userVo, USER_LOGIN_SUCCESS);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public BaseResponse<Integer> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        // 获取数据
        String uuid = userRegisterRequest.getUuid();
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String userEmail = userRegisterRequest.getUserEmail();
        Integer captcha = userRegisterRequest.getCaptcha();
        // 验证两次密码是否一致
        if (!userPassword.equals(checkPassword)) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, USER_REGISTER_FAIL, USER_CHECK_FAIL);
        }
        // 验证邮箱验证码是否一致
        Integer codeResult = userVerificationService.checkVerificationCode(uuid, captcha);
        if (codeResult != 1) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, USER_REGISTER_FAIL, USER_CODE_FAIL);
        }
        // 注册用户
        Integer userId = userService.userRegister(userAccount, userPassword, userEmail);
        if (userId == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, USER_REGISTER_FAIL, USER_REGISTER_REPEAT);
        }
        return ResultUtils.success(userId, USER_REGISTER_SUCCESS);
    }

    @ApiOperation("查询用户日程")
    @GetMapping("/searchschedule/{userId}")
    public BaseResponse<List<ScheduleVo>> searchSchedule(@PathVariable Integer userId) {
        List<UserScheduleVo> userScheduleList = userScheduleService.scheduleSearch(userId);
        List<ScheduleVo> userScheduleVoList = scheduleService.getUserSchedule(userScheduleList);
        return ResultUtils.success(userScheduleVoList, USER_SCHEDULE_GET_SUCCESS);
    }

    @ApiOperation("添加用户日程")
    @PostMapping("/addschedule")
    public BaseResponse<Integer> addSchedule(@RequestBody UserScheduleModifyRequest userScheduleModifyRequest) {
        Integer userId = userScheduleModifyRequest.getUserId();
        Integer scheduleId = userScheduleModifyRequest.getScheduleId();
        Integer inserted = userScheduleService.insertSchedule(userId, scheduleId);
        return ResultUtils.success(inserted, USER_SCHEDULE_ADD_SUCCESS);
    }

    @ApiOperation("取消用户日程")
    @PostMapping("/deleteschedule")
    public BaseResponse<Integer> deleteSchedule(@RequestBody UserScheduleModifyRequest userScheduleModifyRequest) {
        Integer userId = userScheduleModifyRequest.getUserId();
        Integer scheduleId = userScheduleModifyRequest.getScheduleId();
        Integer deletedSchedule = userScheduleService.deleteSchedule(userId, scheduleId);
        return ResultUtils.success(deletedSchedule, USER_SCHEDULE_CANCEL_SUCCESS);
    }

    @ApiOperation("获取用户参展日程")
    @GetMapping("/exhibits/{params}")
    public BaseResponse<List<UserExhibitMomentVo>> getUserExhibits(@PathVariable Integer params) {
        List<UserExhibitMomentVo> userExhibitMomentVoList = userExhibitMomentService.searchUserExhibitMoment(params);
        return ResultUtils.success(userExhibitMomentVoList, "用户参展日程");
    }
}